/*
 * Copyright (c) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include "cardsender_srv_client.h"
#include "string_ex.h"
#include "datetime_ex.h"
#include "ipc_skeleton.h"
#include "iservice_registry.h"
#include "if_system_ability_manager.h"
#include "system_ability_definition.h"
#include "cardsender_errors.h"
#include "cardsender_common.h"
#include "hilog_wrapper.h"

using namespace OHOS;
using namespace std;

namespace SwanLink {
namespace Finance {
CardsenderSrvClient::CardsenderSrvClient() {}
CardsenderSrvClient::~CardsenderSrvClient() {}

ErrCode CardsenderSrvClient::Connect()
{
    std::lock_guard<std::mutex> lock(mutex_);
    if (proxy_ != nullptr) {
        return ERR_OK;
    }
    sptr<ISystemAbilityManager> sysMgr = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
    if (sysMgr == nullptr) {
        CARDSENDER_HILOGE(CARDSENDER_SRV, "failed to GetSystemAbilityManager,%{public}d",
            FINANCE_CARDSENDER_SERVICE_ID);
        return E_GET_SYSTEM_ABILITY_MANAGER_FAILED;
    }
    sptr<IRemoteObject> remoteObject_ = sysMgr->CheckSystemAbility(FINANCE_CARDSENDER_SERVICE_ID);
    if (remoteObject_ == nullptr) {
        CARDSENDER_HILOGE(CARDSENDER_SRV, "failed to GetSystemAbility by %{public}d", FINANCE_CARDSENDER_SERVICE_ID);
        return E_GET_SYSTEM_ABILITY_MANAGER_FAILED;
    }

    deathRecipient_ = sptr<IRemoteObject::DeathRecipient>(new CardsenderSrvDeathRecipient());
    if (deathRecipient_ == nullptr) {
        CARDSENDER_HILOGE(CARDSENDER_SRV, "failed to Create DeathRecipinent,%{public}d", FINANCE_CARDSENDER_SERVICE_ID);
        return ERR_NO_MEMORY;
    }
    if ((remoteObject_->IsProxyObject()) && (!remoteObject_->AddDeathRecipient(deathRecipient_))) {
        CARDSENDER_HILOGW(CARDSENDER_SRV, "Add death recipient to failed,%{public}d", FINANCE_CARDSENDER_SERVICE_ID);
        return E_ADD_DEATH_RECIPIENT_FAILED;
    }

    proxy_ = iface_cast<ICardsenderSrv>(remoteObject_);
    return ERR_OK;
}

void CardsenderSrvClient::ResetProxy(const wptr<IRemoteObject>& remote)
{
    std::lock_guard<std::mutex> lock(mutex_);
    RETURN_IF(proxy_ == nullptr);
    auto serviceRemote = proxy_->AsObject();
    if ((serviceRemote != nullptr) && (serviceRemote == remote.promote())) {
        serviceRemote->RemoveDeathRecipient(deathRecipient_);
        proxy_ = nullptr;
    }
}

void CardsenderSrvClient::CardsenderSrvDeathRecipient::OnRemoteDied(const wptr<IRemoteObject>& remote)
{
    if (remote == nullptr) {
        return;
    }
    CardsenderSrvClient::GetInstance().ResetProxy(remote);
}

int CardsenderSrvClient::Open(const std::string& name, const Transport& trans)
{
    int ret = Connect();
    if (ret != ERR_OK) {
        CARDSENDER_HILOGE(CARDSENDER_JS_NAPI, "open failed");
        return -1;
    }
    return proxy_->Open(name, trans);
}

int CardsenderSrvClient::Close(int handle)
{
    int ret = Connect();
    if (ret != ERR_OK) {
        CARDSENDER_HILOGE(CARDSENDER_JS_NAPI, "close failed");
        return -1;
    }
    return proxy_->Close(handle);
}

int CardsenderSrvClient::Reset(int handle)
{
    int ret = Connect();
    if (ret != ERR_OK) {
        CARDSENDER_HILOGE(CARDSENDER_JS_NAPI, "Reset failed");
        return -1;
    }
    return proxy_->Reset(handle);
}

int CardsenderSrvClient::DispenseCard(int handle)
{
    int ret = Connect();
    if (ret != ERR_OK) {
        CARDSENDER_HILOGE(CARDSENDER_JS_NAPI, "DispenseCard failed");
        return -1;
    }
    return proxy_->DispenseCard(handle);
}

vector<DevAccess> CardsenderSrvClient::GetSupportAccess()
{
    int ret = Connect();
    if (ret != ERR_OK) {
        CARDSENDER_HILOGE(CARDSENDER_JS_NAPI, "GetSupportAccess failed");
        return {};
    }
    return proxy_->GetSupportAccess();
}

int CardsenderSrvClient::QueryStatus(int handle, int *pSuppBinStat, int *pRetainBinStat)
{
    int ret = Connect();
    if (ret != ERR_OK) {
        CARDSENDER_HILOGE(CARDSENDER_JS_NAPI, "QueryStatus failed");
        return -1;
    }
    return proxy_->QueryStatus(handle, pSuppBinStat, pRetainBinStat);
}
}  // namespace Finance
}  // namespace SwanLink
